/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "../guidefaults.h"
#include "../Button.h"

namespace gui {

Button::Button(void) 
:	Label("", gui::CENTER),
	m_bIsToggleButton(false),
	m_bToggled(false),
	m_pGroup(NULL)
{
}

Button::Button(const std::string &text, eTextAlign align)
:	Label(text, align),
	m_bIsToggleButton(false),
	m_bToggled(false),
	m_pGroup(NULL)
{
}

Button::~Button(void)
{
}

void Button::draw()
{
	if (bVisible) {
		vRealPos = getAbsolutePosition();
		
		if (!pTexture) {
			if (m_bHighlighted || m_bToggled) {
				// Draw the highlight if active
				pRenderer->drawFilledRect(vRealPos.x+1, vRealPos.y+1, vDim.x-1, vDim.y-1, vHighlightBackColor);
				pRenderer->drawRoundRect(vRealPos.x, vRealPos.y, vDim.x, vDim.y, vHighlightBorderColor);
			} else {
				// Draw the background
				pRenderer->drawFilledRect(vRealPos.x, vRealPos.y, vDim.x, vDim.y, vBackColor);
				pRenderer->drawRoundRect(vRealPos.x, vRealPos.y, vDim.x, vDim.y, vBorderColor);
			}
		} else {
			//pRenderer->drawRoundRect(vRealPos.x, vRealPos.y, vDim.x, vDim.y, vHighlightBorderColor);
			if ((m_bHighlighted || m_bToggled) && m_pHighlightedTexture) {
				m_pHighlightedTexture->display(vRealPos.x, vRealPos.y, 1.0f, true);
			} else {
				if (bEnabled) {
					pTexture->display(vRealPos.x, vRealPos.y, 1.0f, true);
				} else {
					if (pDisabledTexture)
						pDisabledTexture->display(vRealPos.x, vRealPos.y, 1.0f, true);
				}
			}
		}

		Label::draw();

	}
}

void Button::onMouseEntered(const MouseEvent &me)
{
	if (!bEnabled) return;
	setHighlighted(true);
	Component::onMouseEntered(me);
}

void Button::onMouseLeft(const MouseEvent &me)
{
	setHighlighted(false);
	Component::onMouseLeft(me);
}

void Button::onMouseReleased(const MouseEvent &me) 
{
	if (!bEnabled) return;
	if (m_bIsToggleButton) {
		m_bToggled = !m_bToggled;
	}
	Component::performAction(me.x, me.y, me.button);
	Component::onMouseReleased(me);
}

void Button::setToggleEnabled(bool value)
{
	m_bIsToggleButton = value;
}

const bool Button::isToggleEnabled() const
{
	return m_bIsToggleButton;
}

const bool Button::isToggled() const
{
	return m_bToggled;
}

void Button::setToggled(bool value) 
{
	m_bToggled = value;
}


} // namespace
